
import { showToast , showConfirmDialog } from 'vant';

export default function common(app) {
    app.config.globalProperties.getHashParam = function (name) {
        const w = location.hash.indexOf('?');
        const query = location.hash.substring(w + 1);
        const vars = query.split('&');
        for (let i = 0; i < vars.length; i++) {
          const pair = vars[i].split('=');
          if (pair[0] == name) { return pair[1]; }
        }
        return null;
    },
    app.config.globalProperties.hidePhone = (value) => {
      if(value){
        return value.substring(0,3) + '****' + value.substring(value.length-4)
      }else{
          return '-'
      }
    },
    // 保留后四位
    app.config.globalProperties.lastFour = (value) => {
      if(value){
        return value.substring(value.length - 4,value.length)
      }else{
        return value
      }
    },
    app.config.globalProperties.assetsAddress = 'https://web.anhuichengzan.cn/applet/assets',
    // 提示框
    app.config.globalProperties.showToast = ({message,position}) => {
      console.log(message,'showtoastMes')
      if(!position){
        position = 'bottom';
      }
      showToast({message: message,position: position});
    },
    // 确认框
    app.config.globalProperties.showDialog = ({message,confirm,cancel,title,showCancelButton = true,confirmButtonText = '确认',cancelButtonText = '取消'}) => {
      showConfirmDialog({
        title: title ?? '温馨提示',
        message: message,
        showCancelButton: showCancelButton,
        confirmButtonText: confirmButtonText,
        cancelButtonText: cancelButtonText,
      }).then(() => {
        if(confirm) confirm();
      }).catch(()=>{if(cancel != null) cancel()});
    },
    app.config.globalProperties.addZero = (date) => {
      // 日期加0
      if(date < 10){
        return `0${date}`;
      }
      return date;
    }
    app.config.globalProperties.lineFeed = (value) => {
      if(!value){
        return value;
      }else{
        var reg = value.replace(/\n/g, '&nbsp;');
        return reg;
      }
    }
    app.config.globalProperties.employedStateFilter = (state) => {
      if (state == 1){
        return '办理中'
      }
      if (state == 2){
          return '办理成功'
      }
      if (state == 3){
          return '办理失败'
      }
    }
}